package com.duobaoyu.dby.supply.biz.client.boundary.auth;

import com.duobaoyu.dby.channel.boundary.bean.auth.param.AssessTokenParam;
import com.duobaoyu.dby.channel.boundary.bean.auth.param.BatchRefreshTokenParam;
import com.duobaoyu.dby.channel.boundary.bean.auth.param.RefreshTokenParam;
import com.duobaoyu.dby.channel.boundary.bean.auth.result.AuthTokenResult;
import com.duobaoyu.dby.channel.boundary.template.SupplyChannelTemplate;
import com.duobaoyu.dby.supply.biz.client.boundary.auth.bean.bo.AuthTokenBO;
import com.duobaoyu.dby.supply.biz.client.boundary.auth.bean.dto.AccessTokenDTO;
import com.duobaoyu.dby.supply.biz.client.boundary.auth.bean.dto.BatchRefreshTokenDTO;
import com.duobaoyu.dby.supply.biz.client.boundary.auth.bean.dto.RefreshTokenDTO;
import com.duobaoyu.dby.supply.biz.converter.ChannelAccessTokenConverter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liyang
 * @version 1.0.0
 * @ClassName ChannelAccessTokenBoundaryClient
 * @Description 渠道认证Client
 * @createTime 15:35:00 2023/10/10
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ChannelAccessTokenBoundaryClient {

    private final SupplyChannelTemplate supplyChannelTemplate;
    private final ChannelAccessTokenConverter channelAccessTokenConverter;

    /**
     * 批量刷新token
     * @param batchRefreshToken
     * @return
     */
    public List<AuthTokenBO> batchRefreshChannelToken(BatchRefreshTokenDTO batchRefreshToken) {
        BatchRefreshTokenParam param = channelAccessTokenConverter.toBatchRefreshTokenParam(batchRefreshToken);
        List<AuthTokenResult> authTokenResults = null;
        try {
            authTokenResults = supplyChannelTemplate.openBatchRefreshToken(param);
        } catch (Exception e) {
            log.warn("批量刷新渠道token异常 error => {}", ExceptionUtils.getStackTrace(e));
            throw e;
        }
        return channelAccessTokenConverter.toAuthTokenList(authTokenResults);
    }

    /**
     * 单个渠道刷新token
     * @param refreshToken
     * @return
     */
    public AuthTokenBO refreshChannelToken(RefreshTokenDTO refreshToken) {
        RefreshTokenParam refreshTokenParam = RefreshTokenParam.builder().channelCode(refreshToken.getChannelCode()).configUniqueKey(refreshToken.getConfigUniqueKey()).build();
        AuthTokenResult authTokenResult = supplyChannelTemplate.openRefreshToken(refreshTokenParam);
        return channelAccessTokenConverter.toAuthToken(authTokenResult);
    }

    /**
     * 渠道accessToken认证
     * @param accessToken
     * @return
     */
    public AuthTokenBO accessChannelToken(AccessTokenDTO accessToken) {
        AssessTokenParam assessTokenParam = AssessTokenParam.builder().channelCode(accessToken.getChannelCode()).configUniqueKey(accessToken.getConfigUniqueKey()).build();
        AuthTokenResult authTokenResult = supplyChannelTemplate.authAssessToken(assessTokenParam);
        return channelAccessTokenConverter.toAuthToken(authTokenResult);
    }
}
